package com.mhkj.common.timer.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import com.mhkj.common.enums.MessageType;
import com.mhkj.common.enums.PlanSate;
import com.mhkj.execute.feedback.entity.PlanFeedBack;
import com.mhkj.execute.feedback.service.PlanFeedBackService;
import com.mhkj.revise.plan.entity.PlanNode;
import com.mhkj.revise.professional.service.ProfessionalPlanNodeService;
import com.mhkj.workspace.notify.entity.Notify;
import com.mhkj.workspace.notify.service.NotifyService;
import com.mics.core.system.organizational.bean.Employee;
import com.mics.core.system.organizational.service.OrgService;

/**
 * 
 * 自动确认
 * @author mics
 * @date 2018年8月23日
 * @version 1.0
 */
@Service
public class AutoSureProgress {
	@Resource
	private ProfessionalPlanNodeService professionalPlanNodeService;
	@Resource
	private PlanFeedBackService planFeedBackService;
	@Resource
	private NotifyService notifyService;
	@Resource
	private OrgService orgService;
	@Transactional
	public void sure(){
		List<PlanFeedBack> planFeedBacks = planFeedBackService.listCompelete();
		for(PlanFeedBack planFeedBack :planFeedBacks){
			if(planFeedBack.getIsComplete() == 1 && planFeedBack.getStatus() == 0){ // 填报完成没有确认
					planFeedBack.setStatus(1);
					planFeedBackService.edit(planFeedBack); //自动确认
					PlanNode planNode = planFeedBack.getPlanNode();
					if(planFeedBack.getCurrentProgress() == 100){ //如果填报进度为100%则修改进度状态
						if(planNode.getDelayDay() == 0 || planNode.getOverDay() == 0){
							List<PlanNode> planNodeTemps = professionalPlanNodeService.getSameNameNode(planNode.getId());
							for(PlanNode planNodeTmep:planNodeTemps){
								planNodeTmep.setState(PlanSate.compline.getState());
								planNodeTmep.setCompleteDate(new Date());
								professionalPlanNodeService.edit(planNodeTmep);
							}
						}else{
							List<PlanNode> planNodeTemps = professionalPlanNodeService.getSameNameNode(planNode.getId());
							for(PlanNode planNodeTmep:planNodeTemps){
								planNodeTmep.setState(PlanSate.OverCompline.getState());
								planNodeTmep.setCompleteDate(new Date());
								professionalPlanNodeService.edit(planNodeTmep);
							}
						}
					}
					//发送确认消息
					String content = planNode.getNodeName()+",已经系统确认";
					List<Employee> employees = orgService.getEmployeeByPost(planNode.getPostId());
					//首选第一个人员
					if(employees != null && employees.size() > 0){
						productMessage(MessageType.PROGRESURE,content,1,employees.get(0).getId(),planNode);
					}
			}
		}
	}
	
	/**
	 * 产生消息
	 * @author mics
	 * @date 2018年8月23日
	 * @version 1.0
	 * @param MessageType  消息类型
	 * @param content 消息内容
	 * @param method 消息来源 1、系统发送  2、人员发送
	 * @param planNode  关联计划节点
	 * @param acceptUserId 接受用户
	 */
	@Transactional
	public void productMessage(MessageType messageType,String content,int method,String acceptUserId, PlanNode planNode){
		Notify notify = new Notify();
		notify.setMethod(1);
		notify.setType(messageType.getState());
		notify.setContent(content);
		notify.setAcceptUserId(acceptUserId);
		notify.setPlanNode(planNode);
		notify.setsName("系统");
		notifyService.save(notify);
	}
	
}
